package org.cuatrovientos.erp.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.apache.log4j.Logger;
import org.cuatrovientos.erp.business.RegisterBOInterface;
import org.cuatrovientos.erp.model.Register;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * Controller class form register management
 * 
 * @author VB
 */
@Controller
public class RegisterController {

	// Get log4j logger
	private static final Logger logger = Logger.getLogger(RegisterController.class);

	@Autowired
	private RegisterBOInterface registerBO;

	/**
	 * default constructor
	 */
	public RegisterController() {
		logger.info("register BEAN instantiated");
	}

	/**
	 * handles default /register or /register/list request
	 * 
	 * @param model
	 * @return the name of the view to show
	 * @RequestMapping({"/register","/register/list" )
	 */
	@RequestMapping({ "/register", "/register/list" })
	public ModelAndView registerDefault() {
		logger.info("default");

		Map<String, Object> model = new HashMap<String, Object>();

		List<Register> registerList = registerBO.selectAll();
		model.put("registers", registerList);
		return new ModelAndView("registerList", model);
	}

	/**
	 * handles /register/search request, redirected to default page
	 * 
	 * @param model
	 * @return the name of the view to show
	 * @RequestMapping({"/register/search" )
	 */
	@RequestMapping(method = RequestMethod.POST, value = { "/register/search" })
	public ModelAndView search(@RequestParam(value = "name", defaultValue = "", required = true) String name) {
		logger.info("search");

		Map<String, Object> model = new HashMap<String, Object>();

		List<Register> registerList = registerBO.search(name);
		model.put("registers", registerList);

		return new ModelAndView("registerList", model);
	}

	/**
	 * handles delete register, and redirect to register default
	 * 
	 * @param model
	 * @return the name of the view to show
	 * @RequestMapping({"/register","/register/list" )
	 */
	@RequestMapping(method = RequestMethod.GET, value = { "/register/delete" })
	public String deleteregister(@RequestParam(value = "id", defaultValue = "", required = true) int id) {
		logger.info("delete " + id);

		registerBO.deleteById(id);
		return "redirect:/register";
	}

	/**
	 * handles new register request. In the jsp we are using spring's form:form
	 * tags Spring expects a model called by default 'command' or any name we
	 * set to it: in this case "register"
	 * 
	 * @return the name of the view to show
	 * @RequestMapping({"/register/new" )
	 */
	@RequestMapping(method = RequestMethod.GET, value = { "/register/new" })
	public ModelAndView newregister() {
		logger.info("new register ");
		Register register = new Register();

		return new ModelAndView("Newregister", "register", register);
	}

	/**
	 * handles new register request. In the jsp we are using spring's form:form
	 * tags Spring expects a model called by default 'command' or any name we
	 * set to it.
	 * 
	 * @return the name of the view to show
	 * @RequestMapping({"/register/new" )
	 */
	@RequestMapping(method = RequestMethod.POST, value = { "/register/save" })
	public String newregister(@Valid Register register,BindingResult bindingResult) {
		logger.info("new register " + register.toString());

		// In case of validation errors, go back to form
		if (bindingResult.hasErrors()) {
			Map<String, Object> model = new HashMap<String, Object>();
			model.put("register", register);
			return "Newregister";
		}

		// If data is ok, insert and go on
		registerBO.insert(register);
		return "redirect:/register";
	}

	/**
	 * handles delete register, and redirect to register default
	 * 
	 * @param model
	 * @return the name of the view to show
	 * @RequestMapping({"/register","/register/list" )
	 */
	@RequestMapping(method = RequestMethod.GET, value = { "/register/detail" })
	public ModelAndView registerDetail(@RequestParam(value = "id", defaultValue = "", required = true) int id) {
		logger.info("Detail " + id);
		Map<String, Object> model = new HashMap<String, Object>();
		Register register = registerBO.selectById(id);
		model.put("register", register);

		return new ModelAndView("registerDetail", model);
	}

	/**
	 * handles register update, first part: register loaded
	 * 
	 * @param model
	 * @return the name of the view to show
	 * @RequestMapping({"/register/update" )
	 */
	@RequestMapping(method = RequestMethod.GET, value = { "/register/update" })
	public ModelAndView update(@RequestParam(value = "id", defaultValue = "", required = true) int id) {
		logger.info("update " + id);
		Map<String, Object> model = new HashMap<String, Object>();
		Register register = registerBO.selectById(id);
		model.put("register", register);
		return new ModelAndView("Updateregister", model);

	}

	/**
	 * handles save update register request. In the jsp we are using spring's
	 * form:form tags Spring expects a model called by default 'command' or any
	 * name we set to it.
	 * 
	 * @return the name of the view to show
	 * @RequestMapping({"/register/saveUpdate" )
	 */
	@RequestMapping(method = RequestMethod.POST, value = { "/register/saveupdate" })
	public String saveUpdate(@Valid Register register,BindingResult bindingResult) {
		logger.info("update register " + register.toString());

		// In case of validation errors, go back to form
		if (bindingResult.hasErrors()) {
			Map<String, Object> model = new HashMap<String, Object>();
			model.put("register", register);
			return "Updateregister";
		}

		registerBO.update(register);
		return "redirect:/register";
	}
}
